﻿using System.Security;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Portal.Core.Adapters;
using Portal.Model;
using Portal.Presentation.Providers;
using Portal.Presentation.Services;
using Rhino.Mocks;

namespace Portal.Presentation.Controllers.Tests.Units
{
	[TestClass()]
	public class SharedControllerTests : BaseControllerTests<SharedController>
	{
		private IUserAdapter _adapter;

		protected override void InitializeTest()
		{
			base.InitializeTest();
			_adapter = Mocks.DynamicMock<IUserAdapter>();
		}

		#region Header

		[TestMethod()]
		[ExpectedException(typeof(SecurityException))]
		public void Header_UserIsNull_ThrowsException()
		{
			var controller = GetController();
			Expect.Call(SecurityProvider.GetUser(null)).IgnoreArguments().Return(null);
			Mocks.ReplayAll();

			var result = controller.Header();
		}

		[TestMethod()]
		public void Header_UserIsProvided_ReturnPartialView()
		{
			var logon = "logon";
			var user = Mocks.Stub<IUser>();
			SetupResult.For(user.Login).Return(logon);

			var controller = GetController();
			Expect.Call(SecurityProvider.GetUser(null)).IgnoreArguments().Return(user);
			Expect.Call(_adapter.DisplayName).Return(logon);
			Mocks.ReplayAll();

			var viewResult = controller.Header();

			var result = viewResult as PartialViewResult;
			Assert.AreEqual(logon, result.ViewBag.DisplayName);
		}

		#endregion Header

		protected override SharedController GetController()
		{
			return new SharedController(SecurityProvider, SessionService, _adapter);
		}
	}
}